home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
9-Digit Zip Code Directory
/
9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO
/
z4src.zip
/
DIHDRRD.C
< prev
next >
Wrap
C/C++ Source or Header
|
1995-07-29
|
5KB
|
178 lines
//----------------------------------------------------------------------------
// MODULE DESCRIPTION
//
// Module: dihdrrd.c
// Title: Data File I/O Library
// Notice: John M. Weeder
// Copyright (c) 1993. All rights reserved.
// This module contains proprietary information and should be
// treated as confidential.
//
//----------------------------------------------------------------------------
// MAINTENANCE HISTORY
//
// $Workfile$
// $Revision$
// $Author$
// $Date$
// $Log$
//
//----------------------------------------------------------------------------
// MODULE NARRATIVE
//
//
// This module contains code to read the file header of a data file.
//
// The code in this module should be written entirely in C.
// Do not use any C++ constructs.
//
// This module is portable to:
// DOS 3.X+
// MS Windows 3.X+
// OS/2 2.X+
// OS/2 2.0 PM
// SCO UNIX.
//
// The following compilers are supported:
// MSC 6.0A
// MSC/C++ 7.0
// Borland C++ 3.1 for DOS
// Borland C++ 1.0 for OS/2 2.X
// SCO UNIX cc
//
//----------------------------------------------------------------------------
#include <di.h>
//----------------------------------------------------------------------------
// Description: Read file header of a data file given the table offset.
// Parameters: hpf Physical file handle
// phdr Buffer to read header into.
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
BOOL FN_E DioHeaderRead(HPF hpf, PDATAHDR phdr)
{
Assert(hpf >= 0 && hpf < MAX_PHYSICAL_FILES);
Assert(di.physical[hpf].fUsed);
Assert(phdr);
if (di.physical[hpf].fl & PF_ERROR)
return FALSE;
if (di.physical[hpf].pdatahdr == NULL)
{
di.physical[hpf].pdatahdr = (PDATAHDR)MemAlloc(sizeof(DATAHDR));
if (di.physical[hpf].pdatahdr == NULL)
{
ErrorNoMem();
goto ERROR_EXIT;
}
if (!DioHeaderReadHandle(di.physical[hpf].hf, di.physical[hpf].pdatahdr))
goto ERROR_EXIT;
}
*phdr = *di.physical[hpf].pdatahdr;
if (di.physical[hpf].fWriteable // If file is writeable, destroy cache
&& di.physical[hpf].pdatahdr)
{
MemFree(di.physical[hpf].pdatahdr);
di.physical[hpf].pdatahdr = NULL;
}
return TRUE;
ERROR_EXIT:
if (di.physical[hpf].pdatahdr)
{
MemFree(di.physical[hpf].pdatahdr);
di.physical[hpf].pdatahdr = NULL;
}
di.physical[hpf].fl |= PF_ERROR;
return FALSE;
}
//----------------------------------------------------------------------------
// Description: Read file header of a data file given the file handle.
// This routine exists because at startup, the file has not
// been placed in the physical file table.
// Parameters: hf File handle
// phdr Buffer to read header into.
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
BOOL FN_E DioHeaderReadHandle(HF hf, PDATAHDR phdr)
{
FPOS fsize;
//
// Go to great extents to verify that this is an actual
// and uncorrupted data file. This is the only place that the
// header data is verified. Directory entries are verified as
// they are read.
//
Assert(phdr);
fsize = FileGetSize(hf);
if (fsize < sizeof(DATAHDR)
|| (fsize % sizeof(DATAHDR)) != 0)
{
Error("Data file size is incorrect (%ld bytes).", fsize);
return FALSE;
}
if (!FileRead(hf, (PBYTE)phdr, sizeof(DATAHDR), 0L))
return FALSE;
if (phdr->lId != DFH_ID)
{
Error("Data file is invalid.\nIncorrect file id.");
return FALSE;
}
if (phdr->usVersion < DFH_VERION)
{
Error("Data file is invalid.\nFormat not supported.");
return FALSE;
}
if (phdr->crc != (ULONG)CrcCalc((PBYTE)phdr, sizeof(DATAHDR) - sizeof(ULONG)))
{
Error("Data file is corrupt.\nFile header CRC code does not verify.");
return FALSE;
}
return TRUE;
}
//----------------------------------------------------------------------------
// Description: Release header cache
// Parameters: hpf Physical file handle
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
BOOL FN_E DioHeaderRelease(HPF hpf)
{
Assert(hpf >= 0 && hpf < MAX_PHYSICAL_FILES);
Assert(di.physical[hpf].fUsed);
if (di.physical[hpf].pdatahdr)
{
MemFree(di.physical[hpf].pdatahdr);
di.physical[hpf].pdatahdr = NULL;
}
return TRUE;
}
//----------------------------------------------------------------------------
// Description: Release directory cache
// Parameters:
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
BOOL FN_E DioHeaderReleaseAll(void)
{
BOOL fResult = TRUE;
HPF hpf;
for (hpf = 0; hpf < MAX_PHYSICAL_FILES; ++hpf)
if (di.physical[hpf].fUsed && !DioHeaderRelease(hpf))
fResult = FALSE;
return fResult;
}
//----------------------------------------------------------------------------
//------------------------------- End of File --------------------------------
//----------------------------------------------------------------------------